import {
  startOfWeek,
  subDays,
  startOfMonth,
  endOfMonth,
  startOfYear,
  endOfYear,
  format,
} from "date-fns";

export function useDateRanges() {
  const now = new Date();

  // 获取本周的开始和结束时间
  const getWeekRange = () => {
    // 计算上周的开始和结束日期
    const sevenDaysAgo = subDays(now, 6);
    const formattedNow = subDays(now, 0);
    // 格式化日期
    const start = format(sevenDaysAgo, "yyyy-MM-dd");
    const end = format(formattedNow, "yyyy-MM-dd");
    // const start = format(startOfWeek(now, { weekStartsOn: 1 }), "yyyy-MM-dd"); // 周一作为一周的开始
    // const end = format(endOfWeek(now, { weekStartsOn: 1 }), "yyyy-MM-dd");
    // console.log();
    return { start, end };
  };

  // 获取本月的开始和结束时间
  const getMonthRange = () => {
    const start = format(startOfMonth(now), "yyyy-MM-dd");
    const end = format(endOfMonth(now), "yyyy-MM-dd");
    return { start, end };
  };

  // 获取本季度的开始和结束时间
  const getQuarterRange = () => {
    const quarter = Math.floor((now.getMonth() + 3) / 3);
    const start = format(new Date(now.getFullYear(), (quarter - 1) * 3, 1), "yyyy-MM-dd");
    const end = format(new Date(now.getFullYear(), quarter * 3, 0), "yyyy-MM-dd");
    return { start, end };
  };

  // 获取本年的开始和结束时间
  const getYearRange = () => {
    const start = format(startOfYear(now), "yyyy-MM-dd");
    const end = format(endOfYear(now), "yyyy-MM-dd");
    return { start, end };
  };
  const getDateRange = () => {
    // 获取当前时间
    const now = new Date();

    // 格式化当前时间
    const formattedDate = format(now, "yyyy-MM-dd");
    return formattedDate;
  };

  //格式化当前时间为年月日时分秒
  const getDateTimeRange = () => {
    // 获取当前时间
    const now = new Date();

    // 格式化当前时间为年月日时分秒
    const formattedDate = format(now, "yyyy-MM-dd HH:mm:ss");
    return formattedDate;
  };
  //格式化当前时间为年月日时分秒
  const getDate = () => {
    // 获取当前时间
    const now = new Date();

    // 格式化当前时间为年月日时分秒
    const formattedDate = format(now, "yyyy-MM-dd");
    return formattedDate;
  };

  return {
    getWeekRange,
    getMonthRange,
    getQuarterRange,
    getYearRange,
    getDateRange,
    getDateTimeRange,
    getDate,
  };
}
